package com.dycx.system.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.system.converter.AppVersionConverter;
import com.dycx.system.dto.AppVersionDTO;
import com.dycx.system.entity.AppVersionDO;
import com.dycx.system.mapper.AppVersionMapper;
import com.dycx.system.service.AppVersionService;
import com.dycx.system.vo.AppVersionVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * (AppVersion)表服务实现类
 *
 * @author wuzongbing
 * @since 2024-02-22 15:25:34
 */

@Service
@AllArgsConstructor
public class AppVersionImpl extends ServiceImpl<AppVersionMapper, AppVersionDO> implements AppVersionService {

    private AppVersionMapper appVersionMapper;

    /**
     * 分页查询
     *
     * @param dto 实例对象
     * @return 查询结果
     */
    @Override
    public Page<AppVersionVO> getPage(AppVersionDTO dto) {
        LambdaQueryWrapper<AppVersionDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotNull(dto.getId()), AppVersionDO::getId, dto.getId());
        wrapper.eq(ObjectUtil.isNotNull(dto.getAppId()), AppVersionDO::getAppId, dto.getAppId());
        wrapper.eq(StrUtil.isNotBlank(dto.getVersion()), AppVersionDO::getVersion, dto.getVersion());
        wrapper.orderByDesc(AppVersionDO::getId);

        Page<AppVersionDO> entityPage = page(new Page<>(dto.getPageIndex(), dto.getPageSize()), wrapper);

        Page<AppVersionVO> voPage = AppVersionConverter.entityToVoPage(entityPage);
        voPage.getRecords().forEach(this::setData);

        return voPage;
    }

    /**
     * 查询详情
     *
     * @return 查询结果
     */
    @Override
    public AppVersionVO getNew(int appId) {
        LambdaQueryWrapper<AppVersionDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppVersionDO::getAppId, appId);
        wrapper.orderByDesc(AppVersionDO::getId);
        wrapper.last("limit 1");

        return setData(AppVersionConverter.entityToVo(getOne(wrapper)));
    }

    /**
     * 查询详情
     *
     * @param id 主键
     * @return 查询结果
     */
    @Override
    public AppVersionVO load(Integer id) {
        AppVersionDO entity = getById(id);

        return setData(AppVersionConverter.entityToVo(entity));
    }

    /**
     * 设置数据
     *
     * @param vo 实体
     * @return 查询结果
     */
    public AppVersionVO setData(AppVersionVO vo) {
        return vo;
    }

    /**
     * 添加
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean create(AppVersionDTO dto) {
        AppVersionDO entity = BeanUtil.toBean(dto, AppVersionDO.class);
        return save(entity);
    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean edit(AppVersionDTO dto) {
        AppVersionDO entity = BeanUtil.toBean(dto, AppVersionDO.class);
        return updateById(entity);
    }

    /**
     * 删除
     *
     * @param id 主键
     * @return
     */
    @Override
    public Boolean delete(Integer id) {
        return removeById(id);
    }

    /**
     * 获取最大排序
     *
     * @return 查询结果
     */
    @Override
    public Integer getMaxSort() {
        return appVersionMapper.getMaxSort();
    }
}

